//
//  MSCompanyController.m
//  觅尚
//
//  Created by No.8 on 2019/3/21.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSCompanyController.h"
#import "MSCompanyView.h"
#import "HQPickerView.h"
#import "MSCompany.h"
#import "MSDepartmentModel.h"
#import "MSRole.h"
#import "zySheetPickerView.h"
#import "MSPresentperfectController.h"

@interface MSCompanyController ()<HQPickerViewDelegate>

/** 公司数组*/
@property(nonatomic,strong)NSMutableArray *companyArray;

@property(nonatomic,strong)NSMutableArray *companyTitleArray;

/** 部门数组*/
@property(nonatomic,strong)NSMutableArray *DepartmentArray;
@property(nonatomic,strong)NSMutableArray *DepartmentTitleArray;

/** 部门数组*/
@property(nonatomic,strong)NSMutableArray *roleArray;
@property(nonatomic,strong)NSMutableArray *roleTitleArray;


/** 部门id*/
@property(nonatomic,strong)NSString *busid;

/** 部门id*/
@property(nonatomic,strong)NSString *depid;


/** 角色id*/
@property(nonatomic,strong)NSString *rolid;


/** 创建界面*/
@property(nonatomic,strong)MSCompanyView *CompanyView;


/** 模型*/
@property(nonatomic,strong)MSCompany *CompanyModel;


/** 部门模型*/
@property(nonatomic,strong)MSDepartmentModel *DepartmentModel;

/** 按钮下标*/
@property(nonatomic,strong)NSString *buttonStr;


@property (nonatomic,strong) UILabel *currentLabel;


@end

@implementation MSCompanyController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
//    [self RalodData];
    
    
    self.companyArray = [NSMutableArray new];
    self.companyTitleArray = [[NSMutableArray alloc]init];
    self.DepartmentArray = [NSMutableArray new];
    self.DepartmentTitleArray = [[NSMutableArray alloc]init];
    self.roleArray = [[NSMutableArray alloc]init];
    self.roleTitleArray = [[NSMutableArray alloc]init];
    [self ralodCompany];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.buttonStr = @"";
    [self AddCompanyView];
    
    [self AnimationClick];
    
    
}
- (void)AddCompanyView{
    
    self.CompanyView = [[MSCompanyView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.CompanyView];
    
   
    self.CompanyView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"注册bg"]];
    UIView * ImageView = [[UIView alloc] initWithFrame:self.CompanyView.frame];
    UIImage * image = [UIImage imageNamed:@"注册bg"];
    ImageView.layer.contents = (__bridge id)image.CGImage;
    /** 设置对齐模式*/
    ImageView.layer.contentsGravity = kCAGravityResize;
    [self.CompanyView addSubview:ImageView];
     [self.CompanyView addSubview:self.CompanyView.backButton];
    
    [self.CompanyView addSubview:self.CompanyView.title];
    [self.CompanyView addSubview:self.CompanyView.companyLable];
    [self.CompanyView addSubview:self.CompanyView.companyButton];
    [self.CompanyView addSubview:self.CompanyView.companylineIMG];
    [self.CompanyView addSubview:self.CompanyView.companyArrowIMG];
    
    [self.CompanyView addSubview:self.CompanyView.departmentLable];
    [self.CompanyView addSubview:self.CompanyView.departmentButton];
    [self.CompanyView addSubview:self.CompanyView.departmentlineIMG];
    [self.CompanyView addSubview:self.CompanyView.departmentArrowIMG];
    
    [self.CompanyView addSubview:self.CompanyView.roleLable];
    [self.CompanyView addSubview:self.CompanyView.roleButton];
    [self.CompanyView addSubview:self.CompanyView.roleArrowIMG];
    [self.CompanyView addSubview:self.CompanyView.rolelineIMG];
    
    
    [self.CompanyView addSubview:self.CompanyView.LoginButton];
}

- (void)AnimationClick{
//    [self.CompanyView.companyButton addTarget:self action:@selector(companyButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.CompanyView.companyButton setTitle:[NSString stringWithFormat:@"%@",self.belongName] forState:UIControlStateNormal];
    
    [self.CompanyView.backButton addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.CompanyView.departmentButton addTarget:self action:@selector(departmentButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.CompanyView.roleButton addTarget:self action:@selector(roleButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.CompanyView.LoginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)backButton{
    
    [self dismissViewControllerAnimated:NO completion:nil];
}


////公司按钮
//- (void)companyButtonClick{
//
//
//    if (self.companyTitleArray.count > 0 && self.companyArray.count > 0) {
//        self.currentLabel = self.CompanyView.companyButton.titleLabel;
//        HQPickerView *picker = [[HQPickerView alloc]initWithFrame:self.view.bounds];
//        picker.delegate = self ;
//        picker.customArr = self.companyTitleArray;
//        [self.view addSubview:picker];
//    }
//
//
//}

//部门按钮
- (void)departmentButtonClick{
    
    if (self.DepartmentTitleArray.count > 0 && self.DepartmentArray.count > 0) {
        self.currentLabel = self.CompanyView.departmentButton.titleLabel;
        HQPickerView *picker = [[HQPickerView alloc]initWithFrame:self.view.bounds];
        picker.delegate = self ;
        picker.customArr = self.DepartmentTitleArray;
        [self.view addSubview:picker];
    }
    
   
}

//职位按钮
- (void)roleButtonClick{
    
    WYCLogFunc;
    if (self.roleTitleArray.count > 0 && self.roleArray.count > 0) {
        self.currentLabel = self.CompanyView.roleButton.titleLabel;
        HQPickerView *picker = [[HQPickerView alloc]initWithFrame:self.view.bounds];
        picker.delegate = self ;
        picker.customArr = self.roleTitleArray;
        [self.view addSubview:picker];
    }
    
    
}

//部门
- (void)ralodCompany {
    [SVProgressHUD showWithStatus:@"加载中"];
    
    NSDictionary * params = @{@"busid":self.belongBusid};
    [HTTPRequest POST:MSgetDepartment params:params succeed:^(id responseObject) {
        
//         WYCLog(@"----%@",responseObject);
        [SVProgressHUD dismiss];
        if (CodeStr) {
            
            for (NSDictionary * dict in responseObject[@"data"]) {
               
                MSDepartmentModel  * model = [MSDepartmentModel mj_objectWithKeyValues:dict];
                [self.DepartmentArray addObject:model];
                [self.DepartmentTitleArray addObject:model.stn_alias];
                
            }
            [self.CompanyView.departmentButton setTitle:[self.DepartmentTitleArray firstObject] forState:UIControlStateNormal];
            
        }else{
            
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}


- (void)ralodlastWithId:(NSString *)ID{
    
    self.rolid = ID;
    
}

//职位
- (void)ralodRoleWithId:(NSString *)ID {
    self.depid = ID;
    [SVProgressHUD showWithStatus:@"加载中"];
    NSDictionary *params = @{@"depid":[NSString stringWithFormat:@"%@",ID]};
    [HTTPRequest POST:MSgetRole params:params succeed:^(id responseObject) {
        [SVProgressHUD dismiss];
        if (CodeStr) {
            for (NSDictionary * dict in responseObject[@"data"]) {
                
                MSRole  * model = [MSRole mj_objectWithKeyValues:dict];
                [self.roleArray addObject:model];
                [self.roleTitleArray addObject:model.pst_alias];
                
            }
            
            [self.CompanyView.roleButton setTitle:[self.roleTitleArray firstObject] forState:UIControlStateNormal];
        }else{
            
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        
    }];
    
}


- (void)pickerView:(UIPickerView *)pickerView didSelectText:(NSString *)text didSelectStr:(NSString *)ID{

    
    self.currentLabel.text = text;
    
    if (self.roleArray.count == 0) {
       
        for (MSDepartmentModel *model in self.DepartmentArray) {
            if ([model.stn_alias isEqualToString:text]) {
                [self ralodRoleWithId:model.depid];
            }
        }

    }else{
        
        for (MSRole *model in self.roleArray) {
            if ([model.pst_alias isEqualToString:text]) {
                [self ralodlastWithId:model.rolid];
            }
        }
    }

}

- (void)loginButtonClick{
    WYCLogFunc;
    if ([self.CompanyView.companyButton.titleLabel.text isEqualToString:@"请选择"]) {
        [MBManager showBriefAlert:@"请选择商户"];
        return;
    }
    if ([self.CompanyView.departmentButton.titleLabel.text isEqualToString:@"请选择"]) {
        [MBManager showBriefAlert:@"请选择部门"];
        return;
    }

    if ([self.CompanyView.roleButton.titleLabel.text isEqualToString:@"请选择"]) {
        [MBManager showBriefAlert:@"请选择职位"];
        return;
    }
    MSPresentperfectController * presentvc = [[MSPresentperfectController alloc] init];
    presentvc.msgId = self.msgId;
    presentvc.belongBusid = self.belongBusid;
    presentvc.belongDepid = self.depid;
    presentvc.belongRolid = self.rolid;
    presentvc.CodeTextStr = self.CodeTextStr;
    presentvc.PassTextStr = self.PassTextStr;
    presentvc.iphoneTextStr = self.iphoneTextStr;
    presentvc.RegisterTextStr = self.registrationCode;
    [self presentViewController:presentvc animated:NO completion:nil];
//    NSDictionary * params = @{@"phoneNumber":self.phoneNumber,@"passWord":self.passWord,@"registrationCode":self.registrationCode,@"msgId":self.msgId,@"code":self.code,@"userName":self.userName,@"avatarUrl":self.avatarUrl,@"gender":self.gender,@"birthday":self.birthday,@"province":self.province,@"country":self.country,@"height":self.height,@"speciality":self.speciality,@"otherSpecialties":self.otherSpecialties,@"belongBusid":self.busid,@"belongDepid":self.depid,@"belongRolid":self.rolid};
//
//     WYCLog(@"--------------%@",params);
//    [HTTPRequest POST:MSregister params:params succeed:^(id responseObject) {
//        WYCLog(@"--------------%@",responseObject);
//        WYCLog(@"==========%@",responseObject[@"msg"]);
//        if (CodeStr) {
//            [MBManager showBriefAlert:responseObject[@"msg"]];
//
//
//
//            UIViewController *presenting = self.presentingViewController; //IndexViewController 模态显示的根视图控制器
//            UIViewController *presentingPresenTing = self.presentingViewController.presentingViewController; //LoginViewController 模态显示的根视图控制器的 模态VC
//            UIViewController *presented= self.presentedViewController; //nil 代表被模态显示的VC
//
//
//            [self.presentingViewController.presentingViewController dismissViewControllerAnimated:NO completion:nil];
//
//        }else{
//
//
//        }
//    } failure:^(NSError *error) {
//         WYCLog(@"==========%@",error);
//    }];
//    WYCLog(@"---------%@",params);
}
@end
